package org.jeecg.modules.paycenter.channel.offline;

import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.paycenter.enums.UTypeEnum;
import org.jeecg.modules.paycenter.service.BaseBankService;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWalletRecharge;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 线下支付服务
 * @Author: lm
 * @Date:   2021-11-1
 * @Version: V1.0
 */
@Service
public class OfflineWalletService extends BaseBankService {
    public static  final String CHANNEL = PayChannelEnum.OFFLINE.channelType;
    @Autowired private IHyShippingNoteService hyShippingNoteService;
    @Autowired private IHyShippingNoteBillService hyShippingNoteBillService;
    @Autowired private WalletService walletService;

    public HyShippingNoteBill createBillShippingBill(HyBill bill,HyShippingNote hyShippingNote , HyDriver driver , HyVehicle vehicle , HyDriverBank driverBank , HyCarrier carrier) {
        String shippingNoteBillNum = CodeUtil.getBillNum(UTypeEnum.DRIVER);
        //添加运单支付流水单,分公司到司机
        HyShippingNoteBill shippingNoteBill = new HyShippingNoteBill();
        shippingNoteBill.setBillId(bill.getId());
        shippingNoteBill.setBillNum(shippingNoteBillNum);
        shippingNoteBill.setBillType(bill.getBillType());
        shippingNoteBill.setOrderId(hyShippingNote.getOrderId());
        shippingNoteBill.setShippingId(hyShippingNote.getId());
        shippingNoteBill.setShippingNoteNumber(hyShippingNote.getShippingNoteNumber());
        shippingNoteBill.setCompanyId(hyShippingNote.getCompanyId());
        shippingNoteBill.setPayType(hyShippingNote.getPayType());
        shippingNoteBill.setCarrierId(hyShippingNote.getCarrierId());
        shippingNoteBill.setDriverId(hyShippingNote.getDriverId());
        shippingNoteBill.setCarrier(driver.getDriverName());
        shippingNoteBill.setActualCarrierId(driver.getIdcard());
        shippingNoteBill.setVehicleId(hyShippingNote.getVehicleId());
        shippingNoteBill.setVehicleNumber(vehicle.getVehicleNumber());
        shippingNoteBill.setVehicleColorCode(vehicle.getVehiclePlateColorCode());
        shippingNoteBill.setPayDt(new Date());//支付时间
        shippingNoteBill.setPayChannel(bill.getPayChannel());
        shippingNoteBill.setAmt(bill.getDriverAmt());

        String craccno = "";
        String craccname = "";
        String bankCode = "";
        //司机结算
        if(!"1".equals(bill.getIzCarrierPay())){
            if(driverBank!=null){
                craccno = driverBank.getBankNum();
                craccname = driverBank.getBankName();
                bankCode = driverBank.getBankType();
            }
        }else {
            //车队长
            craccno = carrier.getBankNum();
            craccname = carrier.getBankName();
            bankCode = carrier.getBankType();
        }

        //添加公司支付司机流水单
        shippingNoteBill.setCraccno(craccno);
//            shippingNoteBill.setCraccname(craccname);
        shippingNoteBill.setCraccname(driver.getDriverName());
        shippingNoteBill.setCrlogaccno("");
        shippingNoteBill.setCrProv("");
        shippingNoteBill.setRespCode("4");
        shippingNoteBill.setRespSource("0");
        shippingNoteBill.setBankCode(bankCode);

        return shippingNoteBill;
    }

    @Transactional
    public void payBatch(List<HyBill> hyBills,HyCustomer customer,
                                                Map<String, HyShippingNote> shippingNoteMap,
                                                Map<String, HyDriver> driverMaps,
                                                Map<String, HyDriverBank> driverBankMaps,
                                                Map<String,HyCarrier> carrierMaps,
                                                Map<String, HyVehicle> vehicleMaps, BigDecimal totalAmt) {
        String customerId = customer.getId();
        //扣除货主钱包余额
        HyCustomerWalletRecharge walletRecharge = walletService.exchange(customerId, totalAmt, CHANNEL, "支付运费");

        List<HyShippingNoteBill> hyShippingNoteBills = new ArrayList<>();
        Map<String, HyShippingNoteBill> hyShippingNoteBillMaps = new HashMap<>();
        List<HyShippingNote> hyShippingNotes = new ArrayList<>();
        String realName = UserUtil.getLoginUser().getRName();
        for (HyBill hyBill : hyBills) {
            hyBill.setBillPayStatus("8");
            hyBill.setPayStatus("6");
            hyBill.setPayChannel(CHANNEL);
            hyBill.setPayDt(new Date());//申请运费支付时间
            hyBill.setFkr(realName);
            hyBill.setPayRecordId(walletRecharge.getId());//钱包支出记录id

            HyShippingNote hyShippingNote = shippingNoteMap.get(hyBill.getShippingId());
            if("1".equals(hyBill.getBillType())){
                hyShippingNote.setShippingPayStatus("5");
                hyShippingNote.setPayChannel(CHANNEL);
                hyShippingNote.setPayDt(hyBill.getPayDt());
                hyShippingNotes.add(hyShippingNote);
            }

            HyDriver driver = driverMaps.get(hyShippingNote.getDriverId());
            HyVehicle vehicle = vehicleMaps.get(hyShippingNote.getVehicleId());
            HyDriverBank driverBank = driverBankMaps.get(hyShippingNote.getDriverBankId());
            //车队长
            HyCarrier carrier = null;
            if("1".equals(hyBill.getIzCarrierPay())){
                carrier = carrierMaps.get(hyShippingNote.getCarrierId());
            }
            HyShippingNoteBill shippingNoteBill = this.createBillShippingBill(hyBill, hyShippingNote, driver, vehicle, driverBank, carrier);
            hyShippingNoteBills.add(shippingNoteBill);
            hyShippingNoteBillMaps.put(hyBill.getId(),shippingNoteBill);

        }
        hyShippingNoteBillService.saveBatch(hyShippingNoteBills);

        for (HyBill hyBill : hyBills) {
            HyShippingNoteBill bill = hyShippingNoteBillMaps.get(hyBill.getId());
            hyBill.setShippingBillId(bill.getId());
        }

        if(hyShippingNotes.size() > 0) {
            hyShippingNoteService.updateBatchById(hyShippingNotes);
        }
        hyBillService.updateBatchById(hyBills);
    }

}
